//
//  DiscoverButtonController.m
//  Coco_Music
//
//  Created by 强哥 on 15/6/25.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//
#warning 搜索专辑与歌单等多个页面push的目标
#import "DiscoverButtonController.h"
#import "DiscoverButtonCell.h"
#import "AFN_block.h"
#import "NewSong.h"
#import "UIImageView+WebCache.h"
#import "SearchSpecial.h"
#import "PlayingViewController.h"

@interface DiscoverButtonController ()

@property(nonatomic,retain)UIImageView *hearImage;
@property(retain,nonatomic)UIImageView *hearImageViwe;
@property(nonatomic,copy)NSString *urlID;
@property(nonatomic,retain)NSMutableArray *NewSpecial;
@property(nonatomic,copy)NSString *URL2;
@property(nonatomic,retain)PlayingViewController *playController;

@end

@implementation DiscoverButtonController

-(void)dealloc
{
    [self.hearImage release];
    [self.hearImageViwe release];
    [self.urlID release];
    [self.NewSpecial release];
    [self.URL2 release];
    [self.playController release];
    [self.url release];
//    [self.stringID release];
    [self.allSongArray release];
    [self.allSpecialArray release];
    [self.SpecialSingerPic release];
    [self.SearchSpecialID release];
    [self.DiscoverSecondPicUrl release];
    [self.DiscoverSecondShowUrl release];
    [super dealloc];
    
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.allSpecialArray = [NSMutableArray array];
        self.myTable = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        self.myTable.backgroundColor = [UIColor whiteColor];
        self.myTable.dataSource = self;
        self.myTable.delegate = self;
        [self.view addSubview:self.myTable];
        [self.myTable release];
        self.allSongArray = [NSMutableArray array];
        self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self VisulEffectView];
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中,请稍后";
        [self.HUD show:YES];
        self.NewSpecial = [NSMutableArray array];
        self.playController = [PlayingViewController shareHandleController];
    }
    return self;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

-(void)VisulEffectView
{
    UIVisualEffectView *visualEffct = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    self.hearImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    visualEffct.frame = self.hearImage.frame;
    visualEffct.alpha = 1;
    [self.hearImage addSubview:visualEffct];
    [visualEffct release];
    [self.hearImage release];
    self.hearImageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(self.hearImage.center.x-70, self.hearImage.center.y-70 , 140, 140)];
    _hearImageViwe.layer.cornerRadius = 10;
    _hearImageViwe.layer.masksToBounds = YES;
    [visualEffct addSubview:self.hearImageViwe];
    self.myTable.tableHeaderView = _hearImage;
    [self.hearImageViwe release];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allSongArray.count != 0) {
        return _allSongArray.count;
    }
    if (_allSpecialArray.count != 0) {
        return _allSpecialArray.count;
    }
    if (_NewSpecial.count != 0) {
        return _NewSpecial.count;
    }

    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellSpecia = @"cellSong";
    DiscoverButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSpecia];
    if (cell == nil) {
        cell = [[DiscoverButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSpecia];
    }
    if (_allSongArray.count != 0) {
        cell.SongInfo = [_allSongArray objectAtIndex:indexPath.row];
        cell.NB = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        return cell;
    }
    if (_allSpecialArray.count != 0) {
        cell.SpecialInfo = [_allSpecialArray objectAtIndex:indexPath.row];
        cell.NB = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    }
    if (_NewSpecial.count != 0) {
        cell.NewSpecial = [_NewSpecial objectAtIndex:indexPath.row];
        cell.NB = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    }
    return cell;
}

//重要,点击播放推荐搜索等歌曲播放.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     if (_allSpecialArray.count != 0) {
         [self add:_allSpecialArray indexPath:indexPath.row];
    }
    if (_allSongArray.count != 0) {
        [self add:_allSongArray indexPath:indexPath.row];
      }
    if (_NewSpecial.count != 0) {
        [self add:_NewSpecial indexPath:indexPath.row];
    }

}


//推荐点击Button页面数据
-(void)setUrl:(NSString *)url
{
    if (_url != url) {
        [_url release];
        _url = [url retain];
    }
    NSString *string = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%@",url];
    NSLog(@"%@",string);
    [AFN_block NSString:string Blokc:^(id request) {
        NSMutableArray *array = [request objectForKey:@"data"];
        NSMutableDictionary *dic = [array firstObject];
        NSMutableArray *songArray = [dic objectForKey:@"songlist"];
        NSMutableDictionary *dicImage = [dic objectForKey:@"user"];
        NSString *string = [dicImage objectForKey:@"pic"];
        NSURL *url = [NSURL URLWithString:string];
        [_hearImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
        [_hearImageViwe sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
        NSString *string1 = nil;
        NSString *string2 = nil;
        _stringID = nil;
        
    for (NSMutableDictionary *dic2 in songArray) {
        string1 = [NSString stringWithFormat:@"%@",[dic2 objectForKey:@"song_id"]];
        string2 = [NSString stringWithFormat:@"%@,%@",string1,string2];
        _stringID = [string2 substringToIndex:[string2 length]-7];
    }
        [AFN_block NSString:[NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",_stringID] Blokc:^(id request) {
            NSMutableArray *array = [request objectForKey:@"data"];

            for (NSMutableDictionary *dic in array) {
                NewSong *song = [[NewSong alloc]init];
                [song setValuesForKeysWithDictionary:dic];
                [self.allSongArray addObject:song];
                [song release];
            }
            [self.myTable reloadData];
            [self.HUD hide:YES];
        }];
    }];
}
//搜索专辑界面数据
-(void)setSearchSpecialID:(NSString *)SearchSpecialID
{
    if (_SearchSpecialID != SearchSpecialID) {
        [_SearchSpecialID release];
        _SearchSpecialID = [SearchSpecialID retain];
    }
    [self.allSpecialArray removeAllObjects];
    NSString *URL = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",SearchSpecialID];
    NSLog(@"%@",URL);
    [AFN_block NSString:URL Blokc:^(id request) {
        NSMutableArray *array = [request objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            SearchSpecial *special = [[SearchSpecial alloc]init];
             [special setValuesForKeysWithDictionary:dic];
            [self.allSpecialArray addObject:special];
            [special release];
        }
        [self.myTable reloadData];
        [self.HUD hide:YES];
    }];
    
}

//搜索专辑图片数据
-(void)setSpecialSingerPic:(NSString *)SpecialSingerPic
{
    if (_SpecialSingerPic != SpecialSingerPic) {
        [_SpecialSingerPic release];
        _SpecialSingerPic = [SpecialSingerPic retain];
    }
    
    NSURL *url = [NSURL URLWithString:SpecialSingerPic];
    [_hearImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    [_hearImageViwe sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    [self.myTable reloadData];
    
}


-(void)setDiscoverSecondPicUrl:(NSString *)DiscoverSecondPicUrl
{
    if (_DiscoverSecondPicUrl != DiscoverSecondPicUrl) {
        [_DiscoverSecondPicUrl release];
        _DiscoverSecondPicUrl = [DiscoverSecondPicUrl retain];
    }
    NSURL *url = [NSURL URLWithString:DiscoverSecondPicUrl];
    [self.hearImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    [self.hearImageViwe sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    [self.myTable reloadData];
}

-(void)setDiscoverSecondShowUrl:(NSString *)DiscoverSecondShowUrl
{
    if (_DiscoverSecondShowUrl != DiscoverSecondShowUrl) {
        [_DiscoverSecondShowUrl release];
        _DiscoverSecondShowUrl = [DiscoverSecondShowUrl retain];
    }
    [_NewSpecial removeAllObjects];
    NSString *URL = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%@",DiscoverSecondShowUrl];
       [AFN_block NSString:URL Blokc:^(id request) {
           NSMutableArray *array = [request objectForKey:@"data"];
           NSString *string1 = nil;
           NSString *string2 = nil;
           _stringID = nil;
           _URL2 = nil;
        for (NSMutableDictionary *dic in array) {
          NSMutableArray *array = [dic objectForKey:@"songlist"];
            for (NSMutableDictionary *dic2 in array) {
                string1 = [NSString stringWithFormat:@"%@",[dic2 objectForKey:@"_id"]];
                string2 = [NSString stringWithFormat:@"%@,%@",string1,string2];
                _stringID = [string2 substringToIndex:[string2 length]-7];
            }
        }
           self.URL2 = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPad42C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music",_stringID];
           NSLog(@"%@",_stringID);
           [AFN_block NSString:self.URL2 Blokc:^(id request) {
               NSMutableArray *array = [request objectForKey:@"data"];
               for (NSMutableDictionary *dic in array) {
                   NewSong *new2 = [[NewSong alloc]init];
                   new2.singer_name = [dic objectForKey:@"singer_name"];
                   new2.song_name = [dic objectForKey:@"song_name"];
                   new2.url_list = [dic objectForKey:@"url_list"];
                   new2.audition_list = [dic objectForKey:@"audition_list"];
                   [self.NewSpecial addObject:new2];
                   [new2 release];
               }
               [self.myTable reloadData];
               [self.HUD hide:YES];
           }];
       }];

    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 52;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
